package pkg

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type ConsulClient struct {
	Client *capi.Client
}

func NewConsulclient(addr string) *ConsulClient {
	cfg := capi.DefaultConfig()
	cfg.Address = addr
	client, err := capi.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	return &ConsulClient{Client: client}
}

type Service struct {
	Addr string
	Port int
	Id   string
	Name string
}

func (c *ConsulClient) Regioster(cfg Service) {
	ul := &capi.AgentServiceRegistration{

		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Port:    cfg.Port,
		Address: cfg.Addr,
	}
	c.Client.Agent().ServiceRegister(ul)
}
